<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.qudao" style="width: 220px" clearable filterable placeholder="请选择渠道号进行搜索" class="filter-item" @change="handleFilter">
        <el-option v-for="(item, index) in qudaoArr" :key="index" :label="item" :value="item" />
      </el-select>
      <el-date-picker
        v-model="value1"
        type="daterange"
        align="left"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item pickertime"
        format="yyyy-MM-dd"
        value-format="yyyyMMdd"
        :picker-options="pickerOptions2"
        @change="handleFilter"
      />
      <el-button v-waves plain size="small" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves plain size="small" class="filter-item btn" type="primary" icon="el-icon-refresh" @click="handleCancel">
        重置
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :summary-method="getSummaries"
      show-summary
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="序号" align="center" width="60" type="index" />
      <el-table-column label="渠道号" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.qudao }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日期" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.day }}</span>
        </template>
      </el-table-column>
      <el-table-column label="补量比例" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.price + '%' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="补量UV" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.uv_num }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" class="pageShow" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <pagination v-show="total > 0" layout="prev, pager, next" :small="true" class="pageHidden" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, fetchQudao } from '@/api/admin/history'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'History',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      value1: '',
      qudaoArr: [],
      listQuery: {
        page: 1,
        limit: 20,
        qudao: '',
        start_tm: '',
        end_tm: ''
      },
      pickerOptions2: {
        shortcuts: [{
          text: '昨天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)
            end.setTime(end.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '这一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '上一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 14)
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      countUv: ''
    }
  },
  created() {
    this.getQudao()
    this.getList()
  },
  methods: {
    getQudao() {
      fetchQudao().then(response => {
        this.qudaoArr = response.channel
      })
    },
    getList() {
      this.listLoading = true
      if (this.value1) {
        this.listQuery.start_tm = this.value1[0]
        this.listQuery.end_tm = this.value1[1]
      } else {
        this.listQuery.start_tm = ''
        this.listQuery.end_tm = ''
      }
      fetchList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.countUv = response.s_uv
        this.listLoading = false
      })
    },
    getSummaries() {
      const sums = ['总计', '-', '-', '-', this.countUv]
      return sums
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCancel() {
      this.listQuery.page = 1
      this.listQuery.qudao = ''
      this.value1 = ''
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 0;
    font-size: 16px;
    color: #ccc;
    cursor: pointer;
    user-select: none;
  }
  .pageHidden{
    display: none;
  }
  .btn{
    margin-left:0;
  }
  .fullwidth{
    width: 400px;
    margin-left:50px;
  }
  @media (max-width:550px) {
    .pageShow {
      display: none;
    }
    .pageHidden {
      display: block;
    }
    .fullwidth{
      width:100%;
      margin-left:0;
    }
    .pickertime{
      width: 100%;
    }
  }
</style>
